package com.example.transit.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.example.transit.R;
import com.example.transit.bean.CarlineInfo;


public class AutoCompleteAdapter extends BaseAdapter implements Filterable {
    private ArrayFilter mFilter;
    private List<CarlineInfo> mList;
    private Context context;
    private ArrayList<CarlineInfo> carlineInfoArrayList;

    public AutoCompleteAdapter(List<CarlineInfo> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public int getCount() {

        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = View.inflate(context, R.layout.item_carline, null);

            holder = new ViewHolder();
            holder.car_num = (TextView) view.findViewById(R.id.car_num_tv);
            holder.start_end_loc = (TextView) view.findViewById(R.id.start_end_loc_tv);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        CarlineInfo carlineInfo = mList.get(position);

        holder.car_num.setText(carlineInfo.getCarNum());
        holder.start_end_loc.setText(carlineInfo.getCarStartLoc()+"--"+carlineInfo.getCarEndLoc());
        return view;
    }

    static class ViewHolder {
        public TextView car_num;
        public TextView start_end_loc;

    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (carlineInfoArrayList == null) {
                carlineInfoArrayList = new ArrayList<CarlineInfo>(mList);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<CarlineInfo> list = carlineInfoArrayList;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();
                ArrayList<CarlineInfo> carlineValues = carlineInfoArrayList;
                int count = carlineValues.size();
                ArrayList<CarlineInfo> newValues = new ArrayList<CarlineInfo>(count);
                for (int i = 0; i < count; i++) {
                    CarlineInfo carlineInfo = carlineValues.get(i);
                    if (carlineInfo != null) {
                        if (carlineInfo.getCarNum() != null && carlineInfo.getCarNum().startsWith(prefixString)) {
                            newValues.add(carlineInfo);
                        } else if (carlineInfo.getCarStartLoc() != null && carlineInfo.getCarStartLoc().startsWith(prefixString)) {

                            newValues.add(carlineInfo);
                        }else if(carlineInfo.getCarEndLoc() != null && carlineInfo.getCarEndLoc().startsWith(prefixString)) {
                            newValues.add(carlineInfo);
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            //noinspection unchecked
            mList = (List<CarlineInfo>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }
}